package db

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"go-member/tool"
	"strconv"
)

// InitRedis
// @Description:连接Redis
func InitRedis() *redis.Client {
	host := tool.GetEnvDefault("redis-host", "127.0.0.1")
	port := tool.GetEnvDefault("redis-port", "6379")
	db := tool.GetEnvDefault("redis-db", "0")
	poolSize := tool.GetEnvDefault("redis-pool-size", "10")
	dbInt, _ := strconv.Atoi(db)
	poolSizeInt, _ := strconv.Atoi(poolSize)
	rdb := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%s", host, port),
		Password: "",
		DB:       dbInt,
		PoolSize: poolSizeInt,
	})
	result := rdb.Ping(context.Background())
	if result.Val() != "PONG" {
		fmt.Println("链接Redis失败")
		return nil
	}
	return rdb
}
